<template>
    <el-dialog :title="`${ruleForm.view ? '查看' : ruleForm.id ? '编辑' : '新增'}供应商`" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
        <el-form v-loading="pageLoading" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto" label-position="top">
            <c-form-template lable="基本信息">
                <el-col :span="11">
                    <el-form-item label="经销商名称" prop="name" class="nameBlock">
                        <div class="flex-y-center">
                            <el-input :disabled="ruleForm.view" v-model="ruleForm.name" placeholder="请输入经销商名称">
                                <el-button slot="append" v-if="!ruleForm.view" @click="handleDealerInfo">查询</el-button>
                            </el-input>
                        </div>
                    </el-form-item>
                </el-col>
                <el-col :span="11" :push="2">
                    <el-form-item label="统一社会信用代码" prop="creditno">
                        <el-input :disabled="ruleForm.view" v-model="ruleForm.creditno" placeholder="请输入统一社会信用代码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="企业类型" prop="type">
                        <el-input :disabled="ruleForm.view" v-model="ruleForm.type" placeholder="请输入企业类型"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11" :push="2">
                    <el-form-item label="法人" prop="legal">
                        <el-input :disabled="ruleForm.view" v-model="ruleForm.legal" placeholder="请输入法人"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="手机号" prop="mobile">
                        <el-input :disabled="!!ruleForm.id" v-model="ruleForm.mobile" placeholder="请输入法人手机号"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11" :push="2">
                    <el-form-item label="邮箱" prop="email">
                        <el-input :disabled="!!ruleForm.id" v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="注册资本" prop="registerCapital">
                        <c-number-input :disabled="ruleForm.view" v-model="ruleForm.registerCapital" placeholder="请输入注册资本">
                            <template slot="append"> 万元 </template>
                        </c-number-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11" :push="2">
                    <el-form-item label="成立日期" prop="establishDate">
                        <el-date-picker class="wh" :disabled="ruleForm.view" v-model="ruleForm.establishDate" type="date" placeholder="选择日期" />
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="注册地址" prop="addressDetails">
                        <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 3}" :disabled="ruleForm.view" v-model="ruleForm.addressDetails" placeholder="请输入注册地址"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11" :push="2">
                    <el-form-item label="经营范围" prop="businessScope">
                        <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 3}" :disabled="ruleForm.view" v-model="ruleForm.businessScope" placeholder="请输入经营范围"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="24" v-if="dialogVisible">
                    <el-form-item label="营业执照" prop="certificatePicPath">
                        <c-upload :disabled="ruleForm.view" @change="$refs.ruleForm.validateField('certificatePicPath')" v-model="ruleForm.certificatePicPath" listType="picture-card" :limit="1" :accept="['.jpg','.png','pdf']" :tip="['只能上传jpg/png/pdf文件，且不超过500kb']" />
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if="dialogVisible">
                    <el-form-item label="公司公章章模" prop="surname">
                        <c-upload :disabled="ruleForm.view" @change="$refs.ruleForm.validateField('surname')" v-model="ruleForm.sellerDetail.surname" listType="picture-card" :limit="1" :accept="['.jpg','.png','pdf']" :tip="['只能上传jpg/png/pdf文件，且不超过500kb']" />
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if="dialogVisible">
                    <el-form-item label="授权委托人签字" prop="signaturePrincipal">
                        <c-upload :disabled="ruleForm.view" @change="$refs.ruleForm.validateField('signaturePrincipal')" v-model="ruleForm.sellerDetail.signaturePrincipal" listType="picture-card" :limit="1" :accept="['.jpg','.png','pdf']" :tip="['只能上传jpg/png/pdf文件，且不超过500kb']" />
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if="dialogVisible">
                    <el-form-item label="法定代表人人名章章模" prop="legalSurname">
                        <c-upload :disabled="ruleForm.view" @change="$refs.ruleForm.validateField('legalSurname')" v-model="ruleForm.sellerDetail.legalSurname" listType="picture-card" :limit="1" :accept="['.jpg','.png','pdf']" :tip="['只能上传jpg/png/pdf文件，且不超过500kb']" />
                    </el-form-item>
                </el-col>
            </c-form-template>
            <c-form-template lable="会员信息" v-if=" ruleForm.view && ruleForm.memberInfo">
                <el-col :span="11">
                    <el-form-item label="会员等级" prop="grade">
                        <el-input :disabled="true" v-model="ruleForm.memberInfo.grade"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11" :push="2">
                    <el-form-item label="当前会员金额" prop="currentAmount">
                        <c-money-input :disabled="true" v-model="ruleForm.dealerAmount.currentAmount"><template slot="append"> 元 </template></c-money-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="会员有效期" prop="timeExpiry">
                        <el-input :disabled="true" :value="dateYMDHMSFormat(ruleForm.dealerAmount.timeExpiry)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11" :push="2">
                    <el-form-item label="优惠比例" prop="discountRatio">
                        <el-input :disabled="true" v-model="ruleForm.memberInfo.discountRatio"><template slot="append"> % </template></el-input>
                    </el-form-item>
                </el-col>
            </c-form-template>
            <c-form-template lable="会员充值记录" class="mr-lg ml-lg" v-if=" ruleForm.view &&ruleForm.dealerAmountLog && ruleForm.dealerAmountLog.length > 0 ">
                <el-table :header-cell-style="{ background: '#F1F1F1', color: '#092132' }" :data="ruleForm.dealerAmountLog">
                    <el-table-column label="序号" align="center" width="80" type="index" />
                    <el-table-column label="充值金额" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.amount | thousands }}
                        </template>
                    </el-table-column>
                    <el-table-column label="可用余额" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.postAmount | thousands }}
                        </template>
                    </el-table-column>
                    <el-table-column label="充值日期" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.timeCreate | dateYMDHMSFormat }}
                        </template>
                    </el-table-column>
                </el-table>
            </c-form-template>
        </el-form>
        <span slot="footer">
            <el-button :loading="loading" @click="handleClose">关闭</el-button>
            <el-button :loading="loading" type="primary" @click="handleSubmit">确定</el-button>
        </span>
    </el-dialog>
</template>
<script>
export default {
    components: {},
    data () {
        return {
            dialogVisible: false,
            loading: false,
            pageLoading: false,
            ruleForm: {
                certificatePicPath: [],
                sellerDetail: {}
            },
            rules: {
                name: [
                    { required: true, message: "请输入经销商名称", trigger: "blur" },
                ],
                type: [
                    { required: true, message: "请输入企业类型", trigger: "blur" },
                ],
                addressDetails: [
                    {
                        required: true,
                        message: "请输入注册地址",
                        trigger: "blur",
                    },
                ],
                creditno: [
                    {
                        required: true,
                        message: "请输入统一社会信用代码",
                        trigger: "blur",
                    },
                ],
                legal: [
                    { required: true, message: "请输入法人", trigger: "blur" }
                ],
                registerCapital: [
                    { required: true, message: "请输入注册资本", trigger: "blur" },
                ],
                businessScope: [
                    { required: true, message: "请输入企业经营范围", trigger: "blur" },
                ],
                establishDate: [
                    { required: true, message: "请选择成立日期", trigger: "change" },
                ],
                email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
                mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
                certificatePicPath: [
                    {

                        required: true,
                        message: "请上传营业执照",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    created () {
    },

    methods: {
        handleSubmit () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.$confirm("是否确认此新增供应商", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        this.loading = true;
                        const data = JSON.parse(JSON.stringify(this.ruleForm));
                        this.$post({
                            url: `/sellerInfo/${data.id ? "update" : "add"}`,
                            data,
                        }).then(
                            () => {
                                this.$message.success("添加成功");
                                this.handleClose();
                                this.$parent.$parent.getList();
                                this.loading = false;
                            },
                            () => {
                                this.loading = false;
                            }
                        );
                    });
                }
            });
        },
        handleDealerInfo () {
            if (!this.ruleForm.name) {
                this.$message.warning("请填写经销商名称");
                return;
            }
            this.$get({
                url: "/businessLicense/getBusinesssInfoByKeyword",
                params: {
                    keyword: this.ruleForm.name,
                },
            }).then((res) => {

                const fields = [
                    { form: "name", key: "companyName" },
                    { form: "type", key: "companyType" },
                    { form: "creditno", key: "creditNo" },
                    { form: "legal", key: "legalPerson" },
                    { form: "registerCapital", key: "capital" },
                    { form: "addressDetails", key: "companyAddress" },
                    { form: "establishDate", key: "establishDate" },
                    { form: "businessScope", key: "businessScope" },
                    { form: "mobile", key: "" },
                    { form: "certificatePicPath" },
                ];
                for (let i = 0; i < fields.length; i++) {
                    if (fields[i].key && res[fields[i].key]) {
                        this.$set(this.ruleForm, fields[i].form, res[fields[i].key]);
                    }
                }
                console.log(this.ruleForm.establishDate);
                this.ruleForm.establishDate = this.moment(this.ruleForm.establishDate, 'YYYY-MM-DD HH:mm:ss')._d
            });
        },
        async handleOpen (item) {
            this.dialogVisible = true;
            if (item && item.id) {
                const params = {
                    id: item.id,
                };
                this.pageLoading = true;
                let res = await this.$get({ url: "/sellerAmount/getById", params })
                let amount = await this.$get({ url: "/sellerAmount/getByAmountLong", params })
                this.pageLoading = false;
                this.ruleForm = {
                    ...res.dealerInfo,
                    dealerAmountLog: amount || [],
                    dealerAmount: res.dealerAmount || {},
                    memberInfo: res.memberInfo || {},
                    view: item.view || false,
                    isExamine: item.isExamine || false,
                }

            }
        },
        handleClose () {
            this.ruleForm = {
                certificatePicPath: [],
                sellerDetail: {}
            };
            this.$refs.ruleForm.resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
<style  lang="scss">
.nameBlock .el-input-group__append {
    background-color: #409eff;
    color: #fff;
}
</style>
